package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.constant.QiniuConstant;
import com.pro_one.constant.RedisConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.exception.CheckItemDeleteFailException;
import com.pro_one.exception.OrderAddressDeleteFailException;
import com.pro_one.pojo.OrderAddress;
import com.pro_one.service.OrderAddressService;
import com.pro_one.util.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/orderaddress")
public class OrderAddressController {
    @Reference
    private OrderAddressService orderAddressService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 新增预约地址信息
     *
     * @param orderAddress
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody OrderAddress orderAddress) {
        try {
            orderAddressService.add(orderAddress);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ORDERADDRESS_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ORDERADDRESS_SUCCESS);
    }

    /**
     * 上传图片
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            int index = originalFilename.lastIndexOf(".");
            //截取后缀名
            String suffix = originalFilename.substring(index);
            //生成随机文件名
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(), fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, QiniuConstant.UPLOAD_URL + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 列表查询以及根据条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByCondition")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean) {
        return orderAddressService.findByCondition(queryPageBean);
    }

    /**
     * 查询单个预约地址信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            OrderAddress orderAddress = orderAddressService.findById(id);
            return new Result(true, MessageConstant.GET_ORDERADDRESS_SUCCESS, orderAddress);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERADDRESS_FAIL);
        }
    }

    /**
     * 修改预约地址信息
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderAddress orderAddress) {
        try {
            orderAddressService.edit(orderAddress);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDERADDRESS_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ORDERADDRESS_SUCCESS);
    }

    /**
     * 删除预约地址，如果已有预约信息，则删除失败
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            orderAddressService.delete(id);
            return new Result(true, MessageConstant.DELETE_ORDERADDRESS_SUCCESS);
        } catch (Exception e) {
            if (e instanceof OrderAddressDeleteFailException) {
                return new Result(false, "删除失败,当前地址存在体检预约信息");
            }
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDERADDRESS_FAIL);
        }
    }
}
